package com.moyo.web.admin.controller;

import com.moyo.model.User;
import com.moyo.service.UserService;
import com.moyo.web.common.security.RequirePermissions;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

/**
 * 管理后台用户管理接口
 */
@RestController
@RequestMapping("/admin/user")
@RequirePermissions("user:manage")
public class UserAdminController {
    private final UserService userService = new UserService();

    @GetMapping("/list")
    @RequirePermissions("user:list")
    public List<User> listAll() {
        return userService.listAll();
    }

    @PostMapping("/disable/{id}")
    @RequirePermissions("user:disable")
    public void disableUser(@PathVariable Long id) {
        userService.disableUser(id, getCurrentUserId());
    }

    @PostMapping("/enable/{id}")
    @RequirePermissions("user:enable")
    public void enableUser(@PathVariable Long id) {
        userService.enableUser(id, getCurrentUserId());
    }

    @GetMapping("/{id}")
    @RequirePermissions("user:view")
    public Optional<User> getUser(@PathVariable Long id) {
        return userService.findById(id);
    }

    @PostMapping("/{userId}/roles")
    @RequirePermissions("user:assign:role")
    public void assignRoles(@PathVariable Long userId, @RequestBody List<Long> roleIds) {
        userService.assignRoles(userId, roleIds, getCurrentUserId());
    }

    // TODO: 从JWT中获取当前用户ID
    private Long getCurrentUserId() {
        return 1L; // 临时返回固定值
    }
}
